package util

import (
	"encoding/xml"
	"strconv"

	"gitee.com/zhangycsz/glin/util/apkparser"
)

// 得到apk的基本信息
type ManifestEncoder struct {
	Name        string
	VersionCode int
	VersionName string
	Package     string
}

func (ths *ManifestEncoder) OpenApk(filePath string) error {
	_, _, err := apkparser.ParseApk(filePath, ths)
	return err
}

func (e *ManifestEncoder) EncodeToken(t xml.Token) error {
	switch vv := t.(type) {
	case xml.StartElement:
		if vv.Name.Local == "manifest" {
			for _, attr := range vv.Attr {
				switch attr.Name.Local {
				case "package":
					e.Package = attr.Value
				case "versionCode":
					e.VersionCode, _ = strconv.Atoi(attr.Value)
				case "versionName":
					e.VersionName = attr.Value
				}
			}
		} else if vv.Name.Local == "application" {
			for _, attr := range vv.Attr {
				switch attr.Name.Local {
				case "label":
					e.Name = attr.Value
				}
			}
		}

	}
	//body, _ := json.Marshal(t)
	//fmt.Println(string(body))
	return nil
}

func (e *ManifestEncoder) Flush() error {
	return nil
}
